package com.gs.order.RabbitConfigMQ;

import com.gs.common.constant.UserConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    //创建延时队列，死信队列，交换机绑定

    @Bean("delayQueue")
    public Queue delayQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 60000);
        args.put("x-dead-letter-exchange", UserConstants.DLX_ORDER_EXCHANGE);
        args.put("x-dead-letter-routing-key", UserConstants.DLX_ORDER_ROUTING_KEY);
        return QueueBuilder.nonDurable(UserConstants.DELAY_ORDER_QUEUE).withArguments(args).build();
    }
    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.nonDurable(UserConstants.DLX_ORDER_QUEUE).build();
    }
    @Bean("delayExchange")
    public DirectExchange directExchange() {
        return new DirectExchange(UserConstants.DELAY_ORDER_EXCHANGE, false, false, null);
    }
    @Bean("dlxExchange")
    public DirectExchange dlxExchange() {
        return new DirectExchange(UserConstants.DLX_ORDER_EXCHANGE, false, false, null);
    }
    @Bean
    public Binding delayBinding(@Qualifier("delayQueue") Queue queue, @Qualifier("delayExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(UserConstants.DELAY_ORDER_ROUTING_KEY);
    }

    @Bean
    public Binding dlxBinding(@Qualifier("dlxQueue") Queue queue, @Qualifier("dlxExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(UserConstants.DLX_ORDER_ROUTING_KEY);
    }
}
